﻿//======================================================
//==     (c)2008 aspxcms inc by NeTCMS v1.0              ==
//==          Forum:bbs.aspxcms.com                   ==
//==         Website:www.aspxcms.com                  ==
//======================================================
using System;
using System.Data;
using System.Data.SqlClient;
using NetCMS.DALFactory;
using NetCMS.Model;
using NetCMS.Common;
using System.Text.RegularExpressions;
using System.Text;
using System.Reflection;
using NetCMS.DALProfile;
using NetCMS.Config;

namespace NetCMS.DALSQLServer
{
    public class Message : DbBase, IMessage
    {
        #region Message_box.aspx
        public int sel_msgInfo(string ID,int flag)
        {
            #region
            SqlParameter param = new SqlParameter("@Mid", ID);
            string Sql = null;
            if (flag == 0)
            {
                Sql = "select count(Id) from " + Pre + "user_Message where Mid=@Mid and isRecyle = 0";
            }
            else if (flag == 1)
            {
                Sql = "select count(Mid) from " + Pre + "User_Message where Mid=@Mid";
            }
            else if (flag == 2)
            {
                Sql = "select FileTF from " + Pre + "User_Message where Mid=@Mid";
            }
            else if (flag == 3)
            {
                Sql = "select count(UserNum) from " + Pre + "User_Message where UserNum=@Mid and issDel=0";
            }
            else if (flag == 4)
            {
                Sql = "select count(Id) from " + Pre + "user_Message where Mid=@Mid and issRecyle = 0";
            }
            return (int)DbHelper.ExecuteScalar(CommandType.Text, Sql, param);
            #endregion
        }

        public int update_msgInfo(string ID,int flag)
        {
            #region
            SqlParameter param = new SqlParameter("@Mid", ID);
            string Sql = null;
            if (flag == 0)
            {
                Sql = "update " + Pre + "user_Message set isRecyle='1' where Mid=@Mid";
            }
            else if (flag == 1)
            {
                Sql = "update " + Pre + "User_Message set isRead='1' where Mid=@Mid";
            }
            return DbHelper.ExecuteNonQuery(CommandType.Text, Sql, param);
            #endregion
        }
        public DataTable sel_userMessage(string ID,int flag)
        {
            #region
            SqlParameter param = new SqlParameter("@Mid", ID);
            string Sql = null;
            if (flag == 0)
            {
                Sql = "select isRdel,issDel,Title,Content,Rec_UserNum,FileTF,LevelFlag from " + Pre + "User_Message where Mid=@Mid";
            }
            else if (flag == 1)
            {
                Sql = "select FriendUserNum,UserName from " + Pre + "User_Friend  where UserNum=@Mid";
            }
            else if (flag == 2)
            {
                Sql = "select MessageNum,MessageGroupNum From " + Pre + "User_Group where GroupNumber=@Mid";
            }
            else if (flag == 3)
            {
                Sql = "select UserNum from " + Pre + "sys_User where UserName=@Mid";
            }
            else if (flag == 4)
            {
                Sql = "select FileName,FileUrl from " + Pre + "User_MessFiles where mID=@Mid";
            }
            return DbHelper.ExecuteTable(CommandType.Text, Sql, param);
            #endregion
        }

        public string sel_userInfo(string UserNum)
        {
            SqlParameter param = new SqlParameter("@UserNum", UserNum);
            string Sql = "Select UserGroupNumber,UserName From " + Pre + "sys_User where UserNum=@UserNum";
            return Convert.ToString(DbHelper.ExecuteScalar(CommandType.Text, Sql, param));
        }
        public void del_userMsg(string ID)
        {
            #region
            SqlParameter param = new SqlParameter("@Mid", ID);
            string Sql = "update " + Pre + "user_Message set issDel=1 where Mid=@Mid and UserNum='" + NetCMS.Global.Current.UserNum + "'";
            DbHelper.ExecuteNonQuery(CommandType.Text, Sql, param);

            string Sql1 = "update " + Pre + "user_Message set isRdel=1 where Mid=@Mid and Rec_UserNum='" + NetCMS.Global.Current.UserNum + "'";
            DbHelper.ExecuteNonQuery(CommandType.Text, Sql1, param);

            string Sql2 = "update " + Pre + "user_Message set isRdel=1,issDel=1 where SortType=0 and Mid=@Mid and Rec_UserNum='" + NetCMS.Global.Current.UserNum + "'";
            DbHelper.ExecuteNonQuery(CommandType.Text, Sql2, param);
            #endregion
        }
        #endregion

        #region message_write.aspx
        public void Add(NetCMS.Model.message uc)
        {
            string Sql = "insert into " + Pre + "User_Message(Mid,UserNum,Title,Content,CreatTime,Send_DateTime,SortType,Rec_UserNum,FileTF,LevelFlag,isRead,issDel,isRdel,isRecyle,issRecyle) values(@Mid,@UserNum,@Title,@Content,@CreatTime,@Send_DateTime,@SortType,@Rec_UserNum,@FileTF,@LevelFlag,'0','0','0','0','0')";
            SqlParameter[] param = addParam(uc);
            DbHelper.ExecuteNonQuery(CommandType.Text, Sql, param);
        }

        private SqlParameter[] addParam(NetCMS.Model.message uc)
        {
            #region
            SqlParameter[] param = new SqlParameter[10];
            param[0] = new SqlParameter("@Mid", SqlDbType.NVarChar, 12);
            param[0].Value = uc.Mid;
            param[1] = new SqlParameter("@UserNum", SqlDbType.NVarChar, 15);
            param[1].Value = uc.UserNum;
            param[2] = new SqlParameter("@Title", SqlDbType.NVarChar, 50);
            param[2].Value = uc.Title;
            param[3] = new SqlParameter("@Content", SqlDbType.NText);
            param[3].Value = uc.Content;
            param[4] = new SqlParameter("@CreatTime", SqlDbType.DateTime, 8);
            param[4].Value = uc.CreatTime;
            param[5] = new SqlParameter("@Send_DateTime", SqlDbType.DateTime, 8);
            param[5].Value = uc.Send_DateTime;
            param[6] = new SqlParameter("@SortType", SqlDbType.TinyInt, 1);
            param[6].Value = uc.SortType;
            param[7] = new SqlParameter("@Rec_UserNum", SqlDbType.NVarChar, 15);
            param[7].Value = uc.Rec_UserNum;
            param[8] = new SqlParameter("@FileTF", SqlDbType.TinyInt, 1);
            param[8].Value = uc.FileTF;
            param[9] = new SqlParameter("@LevelFlag", SqlDbType.TinyInt, 1);
            param[9].Value = uc.LevelFlag;
            return param;
            #endregion
        }

        public int add_userMsg(string MfID, string Mid, string UserNum, string fileName, string FileUrl, DateTime CreatTime)
        {
            #region
            SqlParameter[] param = new SqlParameter[6];
            param[0] = new SqlParameter("@MfID",SqlDbType.NVarChar,18 );
            param[0].Value = MfID;
            param[1] = new SqlParameter("@Mid",SqlDbType.NVarChar,18 );
            param[1].Value = Mid;
            param[2] = new SqlParameter("@UserNum",SqlDbType.NVarChar,16 );
            param[2].Value = UserNum;
            param[3] = new SqlParameter("@fileName",SqlDbType.NVarChar,50 );
            param[3].Value = fileName;
            param[4] = new SqlParameter("@FileUrl",SqlDbType.NVarChar,100 );
            param[4].Value = FileUrl;
            param[5] = new SqlParameter("@CreatTime",SqlDbType.DateTime,8 );
            param[5].Value = CreatTime;
            string Sql = "insert into " + Pre + "User_MessFiles(MfID,mID,UserNum,FileName,FileUrl,CreatTime) values(@MfID,@Mid,@UserNum,@fileName,@FileUrl,@CreatTime)";
            return DbHelper.ExecuteNonQuery(CommandType.Text, Sql, param);
            #endregion
        }

        #endregion

        #region Message_red.aspx
        /// <summary>
        /// 清除无用的消息
        /// </summary>
        /// <returns></returns>
        public int clearmessage()
        {
            #region
            int j = 0;
            string Sql = "select ID,Mid from " + Pre + "User_Message where issDel=1 and isRdel=1 order by id desc";
            DataTable dt = DbHelper.ExecuteTable(CommandType.Text, Sql, null);
            if (dt != null && dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    string Sql1 = "delete from " + Pre + "User_Message where id=" + int.Parse(dt.Rows[i]["id"].ToString()) + "";
                    DbHelper.ExecuteNonQuery(CommandType.Text, Sql1, null);

                    string Sql2 = "delete from " + Pre + "user_MessFiles where mID='" + dt.Rows[i]["Mid"].ToString() + "'";
                    DbHelper.ExecuteNonQuery(CommandType.Text, Sql2, null);
                    j++;
                }
            }
            return j;
            #endregion
        }

        public void clearmessagerecyle()
        {
            string Sql1 = "delete from " + Pre + "User_Message where issRecyle=1 and isRecyle=1";
            DbHelper.ExecuteNonQuery(CommandType.Text, Sql1, null);
        }

        #endregion
    }
}
